package csu.fun.spring_store.web.controller;

//用户管理

import com.alibaba.fastjson.JSON;
import csu.fun.spring_store.domain.Account;
import csu.fun.spring_store.persistence.AccountMapper;
import csu.fun.spring_store.persistence.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Validated
@SessionAttributes("Account")
public class AccountController {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private OrderMapper orderMapper;

    @GetMapping("/AccountForm")
    public String AccountForm(Model model)
    {
        List<Account> accounts=accountMapper.selectList(null);
        model.addAttribute("Account",accounts);
        return "AccountManagement.html";
    }

    @PostMapping("/update")
    public String update(@ModelAttribute  Account account,
                       Model  model){
        System.out.println(account.getUserid()+account.getCity());
       accountMapper.setCityByUserid(account.getUserid(),account.getCity());
       accountMapper.setAddr1ByUserid(account.getUserid(),account.getAddr1());
       accountMapper.setAddr2ByUserid(account.getUserid(),account.getAddr2());
       accountMapper.setPhoneByUserid(account.getUserid(),account.getPhone());
       accountMapper.setEmailByUserid(account.getUserid(),account.getEmail());
       accountMapper.setFirstnameByUserid(account.getUserid(),account.getFirstname());
       accountMapper.setLastnameByUserid(account.getUserid(),account.getLastname());
       accountMapper.setZipByUserid(account.getUserid(),account.getZip());
       accountMapper.setCountryByUserid(account.getUserid(),account.getCountry());
       accountMapper.setStateByUserid(account.getUserid(),account.getState());
       accountMapper.setStatusByUserid(account.getUserid(),account.getStatus());

        List<Account> accounts=accountMapper.selectList(null);
        model.addAttribute("Account",accounts);
        return "AccountManagement.html";

    }
    @RequestMapping("/AccountChange")
    public String change(@RequestParam(value = "id")String id,Model model){
        Account account=accountMapper.selectById(id);
        model.addAttribute("loginAccount",account);
        String pass=accountMapper.getpassward(id);
        model.addAttribute("pass",pass);
        JSON json=orderMapper.profile(id);
        model.addAttribute("json",json);
        return "AccountBianji";
    }
    @RequestMapping("/baocun")
    public String baocun(Account account,String password,String favouriteCategoryId,String listOption,String bannerOption, String language){


          accountMapper.updateById(account);
          accountMapper.reset(password,account.getUserid());
          int list,bann;
          if(listOption==null)
          { list=0;}
          else
          {list =1;}
        if(bannerOption==null)
        { bann=0;}
        else
        {bann =1;}
          accountMapper.reprofile(language,favouriteCategoryId,list,bann,account.getUserid());

        return "redirect:AccountForm";
    }
}
